package edu.njust.controller;

import edu.njust.Service.stayregister.StayRegisterService;
import edu.njust.controller.depcontroller.Code;
import edu.njust.controller.depcontroller.Result;
import edu.njust.domain.StayRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/StayRegisterController")
public class StayRegisterController {

    @Autowired
    private StayRegisterService stayRegisterService;

    @GetMapping("/selectBypassengerID/{passengerID}")
    public Result selectBypassengerID(@PathVariable int passengerID){
        List<Integer> userID = stayRegisterService.selectBypassengerID(passengerID);
        return new Result(Code.GET_OK,userID);
    }

    @GetMapping("/selectMoney/{id}")
    public Result selectMoney(@PathVariable int id){
        List<StayRegister> stayRegisters = stayRegisterService.selectMoney(id);
        Integer code = stayRegisters != null ? Code.GET_OK : Code.GET_ERR;
        String msg = stayRegisters != null ? "" : "数据查询失败，请重试！";
        return new Result(code,stayRegisters,msg);
    }

    //Result中返回List<StayRegister>集合
    @GetMapping("/selectMoneyBypassengerID/{passengerID}")
    public Result selectMoneyBypassengerID(@PathVariable int passengerID){
        List<StayRegister> stayRegisters = stayRegisterService.selectMoneyBypassengerID(passengerID);
        Integer code = stayRegisters != null ? Code.GET_OK : Code.GET_ERR;
        String msg = stayRegisters != null ? "" : "数据查询失败，请重试！";
        //System.out.println(stayRegisters);
        return new Result(code,stayRegisters,msg);
    }
    @GetMapping("/getAllMoneyNum/{passengerID}")
    public Result getAllMoneyNum(@PathVariable int passengerID){
        List<StayRegister> stayRegisters = stayRegisterService.selectMoneyBypassengerID(passengerID);
        double allMoneyNum = stayRegisterService.getAllMoneyNum(stayRegisters);
        return new Result(Code.GET_OK,allMoneyNum);
    }

    @GetMapping("/getRoomsBypassengerID/{passengerID}")
    public Result getRoomsBypassengerID(@PathVariable int passengerID){
        List<StayRegister> stayRegisters = stayRegisterService.selectMoneyBypassengerID(passengerID);
        //List<Integer> rooms=new ArrayList<>();
        Map<Integer,Integer> rooms=new HashMap<>();
        for (int i=0;i<stayRegisters.size();i++){
            rooms.put(stayRegisters.get(i).getRoomID(),stayRegisters.get(i).getStayNumber());
        }
        Integer code = rooms != null ? Code.GET_OK : Code.GET_ERR;
        String msg = rooms != null ? "" : "数据查询失败，请重试！";
        return new Result(code,rooms,msg);
    }

    @PutMapping("/changeStayRegistersIsBill/{passengerID}")
    public Result changeStayRegistersIsBill(@PathVariable int passengerID){
        List<StayRegister> stayRegisters = stayRegisterService.selectMoneyBypassengerID(passengerID);
        for (int i=0;i<stayRegisters.size();i++){
            //设置成已完成付款
            stayRegisterService.updatebill(stayRegisters.get(i).getId());
        }
        Integer code = stayRegisters != null ? Code.GET_OK : Code.GET_ERR;
        String msg = stayRegisters != null ? "" : "数据查询失败，请重试！";
        return new Result(code,stayRegisters,msg);
    }


}
